package jp.syuriken.snsw.terenaibot.internal;

import java.util.Arrays;
import java.util.logging.Level;

import jp.syuriken.snsw.terenaibot.PostWrapper2;
import jp.syuriken.snsw.terenaibot.WrapperException2;
import jp.syuriken.snsw.terenaibot.format.CompileArg;
import jp.syuriken.snsw.terenaibot.format.FormatSyntaxException;
import jp.syuriken.snsw.terenaibot.format.PrecompiledSyntaxGroup;
import jp.syuriken.snsw.terenaibot.format.function.AbstractFormatFunction;

/**
 * TODO snsoftware
 * 
 * @author $Author: snswinhaiku.lo@gmail.com $ */
public class UnfollowFunction extends AbstractFormatFunction {
	
	private final PostWrapper2 postWrapper;
	

	/**
	 * インスタンスを生成する。
	 * 
	 * @param postWrapper ポストラッパ
	 */
	public UnfollowFunction(PostWrapper2 postWrapper) {
		this.postWrapper = postWrapper;
	}
	
	@Override
	public String getReturnValue(PrecompiledSyntaxGroup[] args, CompileArg compileArg) throws FormatSyntaxException {
		if (args.length > 1) {
			compileArg.getLogger().log(
					Level.INFO,
					String.format("Function<unfollow> is taken 0 or 1 args, but got %d: %s", args.length,
							Arrays.toString(args)));
		}
		String userId;
		if (args.length == 0) {
			userId = compileArg.getProperty("user", "uniqid");
		} else {
			userId = args[0].format(compileArg);
		}
		
		try {
			postWrapper.follow(userId);
		} catch (WrapperException2 e) {
			compileArg.getLogger().log(Level.INFO, e, "UnfollowFunction: exception during following " + userId);
		}
		
		return "";
	}
	
}
